<style>
.layui-form-pane .layui-form-label{width:auto;}
.layui-form-item{margin-bottom: 0px;}
.order-time-bg{background-color:#1E9FFF;color:white;padding:0px 5px;}
</style>
<div class="fixed-bar">
<form class="layui-form layui-form-pane fixed-bar-form" action="{:url('mp/Workshop/index_v2_ajax')}" method="get" id="hisi-table-search-optable">
	<div class="layui-form-item">	
		<div class="layui-inline">
			<label class="layui-form-label">订单号</label>
			<div class="layui-input-inline">
				<input type="text" name="client_ordernum" lay-verify="" placeholder="输入订单号" class="layui-input">
			</div>
		</div>
		{if env('env_site') == 'riwei' }
		<div class="layui-inline">
			<label class="layui-form-label">客户单号</label>
			<div class="layui-input-inline">
				<input type="text" name="ext_id" lay-verify="" placeholder="输入客户单号" class="layui-input">
			</div>
		</div>
		{/if}
		<div class="layui-inline">
			<label class="layui-form-label">SKU</label>
			<div class="layui-input-inline">
				<input type="text" name="sku" lay-verify="" placeholder="输入SKU" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">所属工厂</label>
			<div class="layui-input-inline">
				{:Form::select('factory_id', '', $factoryKV, '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">订单状态</label>
			<div class="layui-input-inline">
				{:Form::select('order_status', '', ['' => '请选择', 1=>'未采购', 2=>'已采购', 3=>'已裁床', 4=>'已车缝', 5=>'已后整', 6=>'已完成'], '', '')}
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">下单日期</label>
			<div class="layui-input-inline">
				<input type="text" name="create_date_range" readonly="" id="create_date_range" lay-verify="" placeholder="选择下单日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">完成日期</label>
			<div class="layui-input-inline">
				<input type="text" name="over_date_range" readonly="" id="over_date_range" lay-verify="" placeholder="选择完成日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">裁床日期</label>
			<div class="layui-input-inline">
				<input type="text" name="caichuang_over_date_range" readonly="" id="caichuang_over_date_range" lay-verify="" placeholder="选择裁床完成日期" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">搜索</button>
		</div>
	</div>
</form>
</div>

<table id="dataTable" lay-filter='dataTable'></table>

{include file="system@block/layui" /}

<script type="text/html" id="picTpl">
    <span class="lay-pic-container" src="{{ d.sInfo.pic }}">
		<img src="{{ d.sInfo.ori_pic }}" class="lay-pic">
		{{# if(d.status==99){ }}
			<span class="sign"><img src="/static/system/image/sign_delete.png"></span>
		{{# }else if(d.status==2){ }}
			<span class="sign"><img src="/static/system/image/sign_over.png"></span>
		{{# } }}
	</span>
</script>
<script type="text/html" id="skuTpl">
    <span class="copy-item">{{ d.client_ordernum }}</span> <span class="layui-badge layui-bg-blue">{{ layui.tool.formatOrderOType(d.o_type) }}</span><span class="layui-badge layui-bg-orange">{{ layui.tool.formatOrderType(d.type) }}</span><br>
	{if env('env_site') == 'riwei' }
	<span class="copy-item">{{ d.ext_id }}</span> <br>
	{/if}
	<span class="copy-item">{{ d.sku }}</span> <br>
    {{ d.shipment_time_diff }} <br>
    {{ d.create_time_diff }} <br>
    {{# if(d.status == 99){ }}<span class="layui-badge">{{ d.delete_text }}</span> {{# } }}
</script>
<script type="text/html" id="factoryTpl">
	工厂：{{ d.factory_name }}<br>
	分组：{{ layui.tool.showSelect('group_id', d.group_id, d.groupKV, 'group-id', d.id) }}
</script>
<script type="text/html" id="orderNumTpl">
	{{# for(var i in d.osList){ }}
    	{{ d.osList[i].size_name }}： {{ d.osList[i].order_count }} / {{ d.osList[i].shicai_count }} / {{ d.osList[i].jiaohuo_count }} / {{ d.osList[i].daohuo_count }} <br>
	{{# } }}
	总数：{{ d.osStat.order_count }} / {{ d.osStat.shicai_count }} / {{ d.osStat.jiaohuo_count }} / {{ d.osStat.daohuo_count }}
</script>

<script type="text/html" id="swe_cut_makeup">
	{{# for(var i in d.osList){ }}
    	{{ d.osList[i].size_name }}： {{ d.osList[i].sew_cut_count }}  <br>
	{{# } }}
	总数：{{ d.osStat.sew_cut_count }} 
</script>
<script type="text/html" id="orderTimeTpl">
    <div class="{{# if(d.create_time > 0){ }}order-time-bg{{# } }}">下单：{{# if(d.create_time > 0){ }} {{ layui.util.toDateString(d.create_time * 1000) }} {{# } }}</div>
	<div class="{{# if(d.shipment_time > 0){ }}order-time-bg{{# } }}">货期：{{# if(d.shipment_time > 0){ }} {{ layui.util.toDateString(d.shipment_time * 1000) }} {{# } }}</div>    
	<div class="{{# if(d.caigou_over_time > 0){ }}order-time-bg{{# } }}">采购：{{# if(d.caigou_over_time > 0){ }} {{ layui.util.toDateString(d.caigou_over_time * 1000) }} {{# } }}</div>
    <div class="{{# if(d.caichuang_over_time > 0){ }}order-time-bg{{# } }}">裁床：{{# if(d.caichuang_over_time > 0){ }} {{ layui.util.toDateString(d.caichuang_over_time * 1000) }} {{# } }}</div>
    <div class="{{# if(d.chejian_over_time > 0){ }}order-time-bg{{# } }}">车间：{{# if(d.chejian_over_time > 0){ }} {{ layui.util.toDateString(d.chejian_over_time * 1000) }} {{# } }}</div>
    <div class="{{# if(d.tail_over_time > 0){ }}order-time-bg{{# } }}">尾部：{{# if(d.tail_over_time > 0){ }} {{ layui.util.toDateString(d.tail_over_time * 1000) }} {{# } }}</div>
    <div class="{{# if(d.over_time > 0){ }}order-time-bg{{# } }}">总完：{{# if(d.over_time > 0){ }} {{ layui.util.toDateString(d.over_time * 1000) }} {{# } }}</div>
</script>
<script type="text/html" id="orderUserTpl">
	{{ d.caigou_user_name }} <br>
    {{ d.caichuang_user_name }}
</script>
<script type="text/html" id="buttonTpl">
	<div style="line-height:35px;">
		{{# if(d.chejian_status==0){ }}
			<a href="{:url('back')}?order_id={{ d.id }}" title="弹回裁床" class="layui-btn layui-btn-primary layui-btn-sm hisi-tr-del" confirm="您确定要执行该操作？">弹回裁床</a><br>
			<a href="javascript:void(0)" data-url="{:url('setOverTime')}?order_id={{ d.id }}" data-id="{{ d.id }}" data-chejian_plan_over_time="{{ d.chejian_plan_over_time }}" title="设置预计完成时间" class="layui-btn layui-btn-normal layui-btn-sm workshop-set-overtime">设置预期</a><br>
		{{# } }}
		<a href="javascript:void(0)" data-url="{:url('done')}?order_id={{ d.id }}&actiontype=swe_cut_make_up" id="workshop-done-{{ d.id }}" data-id="{{ d.id }}" data-json='{{ d.osJson }}' title="更新车间补裁片数" class="layui-btn layui-btn-sm workshop-done layui-btn-warm">车间补片</a><br />
		
		{{# if(d.chejian_status==0){ }}	
			<a href="javascript:void(0)" data-url="{:url('done')}?order_id={{ d.id }}" id="workshop-done-{{ d.id }}" data-id="{{ d.id }}" data-json='{{ d.osJson }}' title="交货数量录入" class="layui-btn layui-btn-sm workshop-done">车间完成</a><br />
			
		{{# } }}
		
	</div>
</script>
<script type="text/html" id="remarkTpl">
	<div class="plan-over-time-{{ d.id }}">
		{{# if(d.chejian_plan_over_time > 0) { }}
		{{ layui.util.toDateString(d.chejian_plan_over_time * 1000, 'yyyy-MM-dd')}}
		{{# } }}
	</div>
	{{ d.remark?d.remark:'' }}
</script>

<div id="overTimeTpl" class="hide">
	<div id="overTimeControl"></div>
</div>
<div id="doneTpl" class="hide">
	<form class="layui-form layui-form-pane" lay-verify="" action="" method="get">
	<div class="layui-form-item pt20 pl20">
		{{#  for(var i in d.list){ }}
		<div class="layui-inline">
			<label class="layui-form-label">{{ d.list[i].size_name }}</label>
			<div class="layui-input-inline">
				<input type="text" name="number[{{ d.list[i].mid }}]" lay-verify="number|required" placeholder="输入数量" class="layui-input">
			</div>
		</div>
		{{#  } }}
		
		<input type="hidden" name="order_id" value="{{ d.order_id }}">
		<input type="hidden" name="url" value="{{ d.url }}">
		<div class="layui-inline fr">
          <button class="layui-btn" lay-submit lay-filter="done" >确认</button>
        </div>
	</div>
	</form>
</div>

<script type="text/javascript">
	var factoryKV = {:json_encode($factoryKV);};
	var groupKV = {:json_encode($groupKV);};
	groupKV['0'] = '请选择';
	
	layui.use([ 'opTable', 'jquery', 'tool', 'laydate', 'laytpl', 'form'], function() {
		var $ = layui.jquery;
		var laytpl = layui.laytpl;
		var form = layui.form;
		var laydate = layui.laydate;
		
		laydate.render({
		    elem: '#create_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
	    	,change: function(value, date, endDate){
		    	$('#create_date_range').val(value);
            	$('#layui-laydate1').remove();//关闭面板
            }
		});
		laydate.render({
		    elem: '#over_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,change: function(value, date, endDate){
		    	$('#over_date_range').val(value);
            	$('#layui-laydate2').remove();//关闭面板
            }
		});
		laydate.render({
		    elem: '#caichuang_over_date_range'
		    ,range: '~'
		    ,theme: 'molv'
		    ,mark: {'{:$today}':''}
		    ,change: function(value, date, endDate){
		    	$('#caichuang_over_date_range').val(value);
            	$('#layui-laydate3').remove();//关闭面板
            }
		});
		
		var opTable = layui.opTable.render({
			elem : '#dataTable'
			,url : "{:url('mp/Workshop/index_v2_ajax')}"
			,page: true
			,limit: 20
            ,limits: [20,50,100,150,200]
            ,text: {
                none : '暂无相关数据'
            }
			,openVisible: false
			,toolbar: '#toolbar'
	        ,defaultToolbar: ['filter', 'exports', 'print']
			,cols : [[ 
				{field:'pic',title : '图片', templet:'#picTpl',width:110}
				,{field:'client_ordernum',title : '订单号/SKU/时效', templet:'#skuTpl',width:180}
				,{field:'factory_id',title : '工厂/分组', templet:'#factoryTpl',width:220}
				,{field:'order_num',title : '下单数/裁床数/车间数/出货数', templet:'#orderNumTpl',width:210}
				,{field:'order_num',title : '车间补裁片数', templet:'#swe_cut_makeup',width:120}
				,{field:'order_time',title : '时间进度', templet:'#orderTimeTpl',width:220}
				,{field:'order_user',title : '采购执行人/裁床执行人', templet:'#orderUserTpl'}
				,{field:'remark',title : '预计完成时间/备注',templet:'#remarkTpl'}
				,{field:'',title : '操作',templet:'#buttonTpl',width:100}
			]]
		});
		
		// 设置分组
		$(document).on('change','.group-id',function(){
			var groupId = $(this).val();
			var orderId = $(this).attr('data-key');
			if (groupId > 0) {
				$.ajax({
		 		   type: "POST",
		 		   url: "{:url('order/setGroupId', ['_t' => 2])}",
		 		   data: "group_id="+groupId+"&order_id="+orderId,
		 		   dataType:"json",
		 		   success: function(res){
		 			  if (res.code == 1) {
						 layer.msg(res.msg, {icon: 1});
					  } else {
						 layer.msg(res.msg, {icon: 2,anim: 6});
					  }
		 		   }
		 		});
			}
		});
		
		// 车间完成
		var layerDone = null;
		$(document).on('click','.workshop-done',function(){
			var orderId = $(this).attr('data-id');
			var url = $(this).attr('data-url');
			var json = $(this).attr('data-json');
			var title=$(this).attr("title");
			var d = [];
			d['list'] = eval("(" + json+ ")");
			d['url'] = url;
			d['order_id'] = orderId;
			var content = laytpl($('#doneTpl').html()).render(d);
			layerDone = layer.open({
				type: 1
				,shade: 0
				,offset: '100px'
				,anim: parseInt(Math.random()*(6+1),10)
				,skin:'layui-layer-molv'
				,title:title
				,content: content
			});
			if(title=='交货数量录入'){
				$(".layui-layer-title").removeClass("layui-bg-orange");
			}else{
				$(".layui-layer-title").addClass("layui-bg-orange");
			}
		});
		form.on('submit(done)', function (data) {
			console.log(data.field);
			var url = data.field.url;
			$.ajax({
	 		   type: "POST",
	 		   url: url,
	 		   data: data.field,
	 		   dataType:"json",
	 		   success: function(res){
	 			  if (res.code == 1) {
	 				 $('#doneTpl form')[0].reset();
	 				 layer.close(layerDone);
					 layer.msg(res.msg, {icon: 1,end:function(){opTable.reload();}});
				  } else {
					 layer.msg(res.msg, {icon: 2,anim: 6});
				  }
	 		   }
	 		});
			
			return false;
		});
		
		// 预计完成时间
		$(document).on('click','.workshop-set-overtime',function(){
			var url = $(this).attr('data-url');
			var chejianPlanOverTime = parseInt($(this).attr('data-chejian_plan_over_time'));
			var id = $(this).attr('data-id');
			date = chejianPlanOverTime > 0? layui.util.toDateString(chejianPlanOverTime *1000, 'yyyy-MM-dd') : '';
			console.log(date);
			var content = laytpl($('#overTimeTpl').html()).render({});
			var layerOverTime = layer.open({
				type: 1
				,shade: 0
				,offset: '100px'
				,anim: parseInt(Math.random()*(6+1),10)
				,area: ['276px', '368px']
				,skin:'layui-layer-molv'
				,title:'预计完成时间'
				,content: content
				,success: function(){
					laydate.render({
					    elem: '#overTimeControl'
					    ,position: 'static'
					    ,btns: false
						,value: date
						,mark: {'{:$today}':''}
				    	,change: function(value, date){ //监听日期被切换
				    		$.ajax({
				 	 		   type: "POST",
				 	 		   url: url,
				 	 		   data: 'date='+value,
				 	 		   dataType:"json",
				 	 		   success: function(res){
				 	 			  if (res.code == 1) {
				 	 				 $('.plan-over-time-'+id).html(value);
				 	 				 layer.close(layerOverTime);
				 					 layer.msg(res.msg, {icon: 1});
				 				  } else {
				 					 layer.msg(res.msg, {icon: 2,anim: 6});
				 				  }
				 	 		   }
				 	 		});
				    	}
					});
				}
			});
		});

		
		/**
	     * layui非静态table搜索渲染
	     * @attr data-table table容器ID
	     * @attr hisi-data table基础参数
	     * @attr action 搜索请求地址
	     */
	   $(document).on('submit', '#hisiSearchOpTable,#hisi-table-search-optable', function() {
	        var that = $(this), 
	            arr = that.serializeArray(), 
	            where = new Array(),
	            dataTable = that.attr('data-table') ? that.attr('data-table') : 'dataTable',
	            options = new Function('return '+ that.attr('hisi-data'))() || {page: {curr:1}};
	        
	            for(var i in arr) {
	                where[arr[i].name] = arr[i].value;
	            }
	        
	        options.url = that.attr('action');
	        options.where = where;
	        
	        opTable.reload(options);
	        return false;
	    });
		
	});
</script>